//video_article_detail

exports.video_article_detail = function (req, res) {

    //获取restful参数
    var restfulParams = req.routeInfo.restfulParams;
    //最终结果
    var resultData = {};

    var contentData = 0;

    var limit = restfulParams.limit == undefined ? config.publicConfig.LIMIT : restfulParams.limit;

    var skip = restfulParams.skip == undefined ? 0 : restfulParams.skip;

    //任务流创建
    async.waterfall([

        //获取文章详情
        function (cb) {

            R.SEND_HTTP(
                req,
                {
                    url: CORE_APIConfig.coreAPI.search_content + "/fc_id/" + restfulParams.fc_id + "/skip/" + skip + "/limit/" + limit,
                    method: 'GET',
                    data: {
                        fields: ["fc_id", "fc_fcid", "fc_title", "fc_content", "u_nickname", "u_header_url", "fc_create_time", "fc_browse_count", "fc_type"].join(",")
                    }

                },
                function (err, data) {

                    resultData.contentData = data.data;
                    contentData = data.data;
                    cb();
                }
            );
        },
        //相关视频
        function (cb) {

            R.SEND_HTTP(
                req,
                {
                    url: CORE_APIConfig.coreAPI.search_content + "/fc_type/" + contentData[0].fc_type + "/fc_fcid/" + contentData[0].fc_fcid + "/limit/3",
                    method: 'POST',
                    data: {
                        fields: ["fc_id", "fc_title", "fc_content", "fc_create_time", "fc_browse_count", "fc_type"].join(",")
                    }

                },
                function (err, data) {

                    var likeContentData = data.data;

                    var pushData = likeContentData;
                    if (likeContentData.length > 0) {
                        for (var i = 0; i < likeContentData.length; i++) {
                            if (likeContentData[i].fc_id == contentData[0].fc_id) {
                                pushData.splice(i,1);
                            }
                        }

                        if(pushData.length == 3){
                            pushData.splice(0,1);
                        }
                        resultData.pushData = pushData;

                        cb();

                    }else{
                        cb();
                    }
                }
            );
        },

        //获取文章评论
        function (cb) {

            R.SEND_HTTP(
                req,
                {
                    url: CORE_APIConfig.coreAPI.search_comment + "/fcc_fcid/" + restfulParams.fc_id + "/fcc_fid/0",
                    method: 'POST',
                    data: {
                        fields: [
                            "fcc_id",
                            "fcc_fcid",
                            "fcc_content",
                            "fcc_uid",
                            "fcc_reply_uid",
                            "fcc_is_reply",
                            "fcc_zan_count",
                            "fcc_is_author_commnet",
                            "fcc_create_time",
                            "fcc_fid",
                            "u_nickname",
                            "u_type",
                            "u_header_url"
                        ].join(",")
                    }

                },
                function (err, data) {

                    //console.log(data);

                    resultData.commentData = data.data;

                    cb();
                }
            );
        },
        //查询当前用户是否对文章评论点赞
        function (cb) {

            //创建添加任务
            var tasks = [];

            //循环节点列表
            resultData.commentData.forEach(function (item, i) {

                //创建每一个插入任务
                tasks.push(function (cb) {

                    //查询对该用户的回复
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.searchContentCommentZanRecord + "/is_delete/0/fcczr_fcid/" + item.fcc_id + "/fcczr_uid/" + restfulParams.u_id,
                            method: 'POST',
                            data: {
                                fields: ["fcczr_id"].join(",")
                            }
                        },
                        function (err, data) {

                            var is_exist = data.data;

                            if (is_exist.length <= 0) {

                                item.isZan = 0;

                            } else {

                                item.isZan = 1;

                            }
                            cb();
                        });
                });
            });
            async.waterfall(tasks, cb);

        },
        //查询该用户下的评论
        function (cb) {

            //创建添加任务
            var tasks = [];

            //循环节点列表
            resultData.commentData.forEach(function (item, i) {

                //创建每一个插入任务
                tasks.push(function (cb) {

                    //查询对该用户的回复
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.search_comment + "/fcc_fid/" + item.fcc_id + "/count/1",
                            method: 'POST',
                            data: {
                                fields: [
                                    "fcc_id",
                                    "fcc_content",
                                    "u_nickname"
                                ].join(",")
                            }

                        },
                        function (err, data) {

                            item.replyCount = data.count;
                            item.replyData = data.data;

                            cb();
                        });

                });

            });

            async.waterfall(tasks, cb);

        },
        //查询当前用户是否点过赞
        function (cb) {
            if (restfulParams.u_id) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchContentZanRecord + "/is_delete/0/fczr_fcid/" + restfulParams.fc_id + "/fczr_uid/" + restfulParams.u_id,
                        method: 'POST',
                        data: {
                            fields: ['fczr_id'].join(",")
                        }

                    },
                    function (err, data) {

                        var is_zan = data.data;

                        if (is_zan <= 0) {
                            resultData.isZanData = 0;
                        } else {
                            resultData.isZanData = 1;
                        }
                        cb();
                    }
                );
            } else {
                cb();
            }

        },
        function (cb) {
            if (restfulParams.u_id) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.userSearchCollect + "/is_delete/0/uc_type/2/uc_fid/" + restfulParams.fc_id + "/uc_uid/" + restfulParams.u_id,
                        method: 'POST',
                        data: {
                            fields: ['uc_id'].join(",")
                        }

                    },
                    function (err, data) {

                        var is_zan = data.data;

                        if (is_zan.length <= 0) {
                            resultData.isCollectData = 0;
                        } else {
                            resultData.isCollectData = 1;
                        }
                        cb();
                    }
                );
            } else {
                cb();
            }

        },


    ], function (err, result) {

        //返回最终拼接结果
        RES.response(res, true, resultData);

    });

};